package com.pc.web.member;

import java.io.UnsupportedEncodingException;
import java.util.List;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.pc.web.HomeController;

@Controller
@RequestMapping(value = "/member")
public class MemberController {	
	@Autowired MemberService service;
	@Autowired MemberDao dao;
	
	// 로그인 확인
	@RequestMapping(value = "/loginCheck", method = RequestMethod.GET)
	public String loginCheck(MemberBeans beans, Model model, HttpSession session) {
		if (service.userSession(beans, session)) { 
			return "redirect:/";
		} else { 
			model.addAttribute("msg", "회원정보X");
			return "member/member_msg"; 
		}
	}
	// 로그아웃
	@RequestMapping(value = "/logout", method = RequestMethod.GET)
	public String logout(HttpSession session) {
		service.removeUser(session);
		return "home";
	}
	// 회원가입 폼 페이지 이동
	@RequestMapping(value = "/joinForm", method = RequestMethod.GET)
	public String joinForm(Model model) {
		service.joinList(model);		
		return "member/member_joinForm";
	}
	// 회원가입 아이디 체크 페이지
	@RequestMapping(value = "/idCheck", method = RequestMethod.GET)
	public String idCheck(MemberBeans beans, Model model) {
		service.idCheck(beans,model);		
		return "member/member_idCheck";
	}
	//회원가입
	@RequestMapping(value = "/join", method = RequestMethod.POST)
	public String join(MemberBeans beans, Model model,HttpSession session) {
		service.join(beans,session,model);
		return "member/member_msg";
	}
	//우편번호 찾기 페이지
	@RequestMapping(value = "/zipcode", method = RequestMethod.GET)
	public String zipcode() {
		return "member/member_zipcode";
	}
	//회원정보 수정 페이지
	@RequestMapping(value = "/updateForm", method = RequestMethod.GET)
	public String updateForm(Model model, HttpSession session,MemberBeans beans){
		service.joinList(model);		
		service.getUserProfile(model,session,beans);		
		return "member/member_updateForm";
	}
	//회원정보 수정
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public String update(MemberBeans beans,Model model){
		service.update(beans,model);
		return "member/member_msg";
	}
	//회원탈퇴 페이지
	@RequestMapping(value = "/leaveForm", method = RequestMethod.GET)
	public String leaveForm(){
		return "member/member_leaveForm";
	}
	//회원탈퇴
	@RequestMapping(value = "/leave", method = RequestMethod.POST)
	public String leave(MemberBeans beans,Model model,HttpSession session){
		service.leave(beans,model,session);
		return "member/member_msg";
	}

	//회원 아이디 찾기 페이지
	@RequestMapping(value = "/idFindForm", method = RequestMethod.GET)
	public String idFindForm(){		
		return "member/member_idFindForm";
	}
	//회원 아이디 찾기
	@RequestMapping(value = "/idFind", method = RequestMethod.POST)
	public String idFind(MemberBeans beans,Model model){	
		service.idFind(beans,model);
				
		return "member/member_idFind";
	}
	//회원 비밀번호 찾기 페이지
	@RequestMapping(value = "/passFindForm", method = RequestMethod.GET)
	public String passFindForm(Model model){
		List<String> emailCode = dao.emailCode();
		model.addAttribute("emailcode", emailCode);		
		return "member/member_passFindForm";
	}
	//회원 비밀번호 찾기
	@RequestMapping(value = "/passFind", method = RequestMethod.POST)
	public String passFind(MemberBeans beans,Model model){
		service.passFind(beans,model);
		return "member/member_passFind";
	}
	//회원 목록 페이지
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list(Model model,MemberBeans beans){
		service.list(model,beans);
		return "member/member_list";
	}
	//회원 상태 변경
	@RequestMapping(value = "/statusChange", method = RequestMethod.GET)
	public String statusChange(Model model,MemberBeans beans){
		dao.statusChange(beans);
		return this.list(model,beans);
	}
}
